import MaterialFactory from "./MaterialFactory.js"
import MaterialTypes from "./MaterialTypes.js"
import * as Cesium from "cesium";

export default class FlowWall {
    constructor(viewer, positions, style, name) {
        this.viewer = viewer;
        style = style || {};
        this.name = name;
        this.wallHeight = style.wallHeight || 200;
        style.materialType = style.materialType || 1;

        this.setPositions(positions);
        //流动墙体单独处理
        if (style.materialType === MaterialTypes.LINEFLOW) {
            this.flowWallMaterial = MaterialFactory.createMaterial(style.materialType, {
                color: style.wallColor || Cesium.Color.RED,
                duration: style.duration || 1000,
            });
            this.addFlowWall();
            // 围墙材质
            this.wallMaterial = MaterialFactory.createMaterial(MaterialTypes.BASEGRADIENT, {
                color: style.wallColor || Cesium.Color.RED,
            });
            this.addWall();
            return;
        }
        //围墙材质
        this.wallMaterial = MaterialFactory.createMaterial(style.materialType, {
            color: style.wallColor || Cesium.Color.RED, //渐变 走马灯可用
            duration: style.duration || 1000,
            count: style.count || 2, //走马灯可用
            colorStops: style.colorStops, //线性渐变时可用
        });
        this.addWall();
    }

    //设置点串
    setPositions(value) {
        this.positions = value ? value : [];
        this.initHeights();
    }

    //初始化高度
    initHeights() {
        let minimumHeights = []; //最小高度集合
        let maximumHeights = []; //最大高度集合
        this.positions.forEach(position => {
            const cartographic = Cesium.Cartographic.fromCartesian(position);
            minimumHeights.push(cartographic.height);
            maximumHeights.push(cartographic.height + this.wallHeight);
        });
        this.minimumHeights = minimumHeights;
        this.maximumHeights = maximumHeights;
    }

    //添加墙体
    addWall() {
        this.wallEntity = this.viewer.entities.add({
            wall: {
                positions: this.positions,
                minimumHeights: this.minimumHeights,
                maximumHeights: this.maximumHeights,
                material: this.wallMaterial
            }
        });
    }

    //添加动态墙体
    addFlowWall() {
        this.flowWallEntity = this.viewer.entities.add({
            wall: {
                positions: this.positions,
                minimumHeights: this.minimumHeights,
                maximumHeights: this.maximumHeights,
                material: this.flowWallMaterial
            }
        });
    }

    //移除墙体对象
    remove() {
        this.viewer.entities.remove(this.wallEntity);
        this.viewer.entities.remove(this.flowWallEntity);
    }

    // 隐藏墙体对象
    hide() {
        if (this.wallEntity) {
            this.wallEntity.show = false;
        }
        if (this.flowWallEntity) {
            this.flowWallEntity.show = false;
        }
    }

    show() {
        if (this.wallEntity) {
            this.wallEntity.show = true;
        }
        if (this.flowWallEntity) {
            this.flowWallEntity.show = true;
        }
    }
}
